// JavaScript Document

/***** FUNCTIES GEBRUIKT DOOR categorie.html *****/

function laadCategorie()
{
	/** laad alle films uit geselecteerde categorie in */
	
	var categorie = getCookie('categorie');	//huidige categorie opvragen
	
	if(categorie == "")	//als er geen categorie is ingesteld, terugvallen op klassiekers
	{
		categorie = "klass";
	}

	console.log(categorie);
	document.getElementById(categorie).className = "actief";	//bijhorende menulink op actief zetten
	
	
	var tmp = xmlDoc.getElementsByTagName('film');	//films opvragen uit XML
	
	var catFilms = [];
	
	for(i = 0; i < tmp.length; i++)
	{
		if(tmp[i].getElementsByTagName('categorie')[0].childNodes[0].nodeValue == categorie)
		{
			/* alle films die de gekozen categorie hebben */
			catFilms.push(tmp[i]);
		}
	}
	
	console.log(catFilms);
	
	var urls = [];
	
	if(base_url == "")
	{
		setBaseURL();
	}
	
	for (i = 0; i < catFilms.length; i++)
	{
		/* URL's aanmaken voor JSON requests */
		urls.push("https://api.themoviedb.org/3/movie/" + catFilms[i].getElementsByTagName("tmdb_id")[0].childNodes[0].nodeValue + "?api_key=" + api_key);
	}

	
	var jxhr = [];
	var result = [];
	
	$.each(urls, function (i, url) {
		jxhr.push(
			$.getJSON(url, function (json) {
				result.push(json);
			})
		);
	});
	
	$.when.apply($, jxhr).done(function() {

		/* indien JSON request(s) gelukt, data invullen in correcte element */
		for(i = 0; i < result.length; i++)
		{
			var titel = result[i].title;
			var txt = "<a href=\"film_detail.html\" onClick=\"setCookie('film_id', '" + result[i].id + "')\">";
			txt += "<img alt=\"" + result[i].title + "\" src=\"" + base_url + "w500" + result[i].backdrop_path + "\" width=\"500\" height=\"281\">";
			var img = new Image();
			img.src = base_url + "w1280" + result[i].backdrop_path; //preloading voor film_detail
			txt += "<div class=\"info_film\">"
			txt += "<section>";
			for(j = 0; j < catFilms.length; j++)
			{
				if(catFilms[j].getElementsByTagName("tmdb_id")[0].childNodes[0].nodeValue == result[i].id)
				{
					txt += "<p>" + catFilms[j].getElementsByTagName("dag")[0].childNodes[0].nodeValue.maakAfkorting() + "&nbsp;" + catFilms[j].getElementsByTagName("uur")[0].childNodes[0].nodeValue;
					txt += "&nbsp;-&nbsp;Screen " + catFilms[j].getElementsByTagName("zaal")[0].childNodes[0].nodeValue + "</p>";	
					var alt = catFilms[j].getElementsByTagName("alt_title")[0].childNodes[0].nodeValue;
					//console.log(alt);
					if(alt != "null")
					{
						titel = alt;
					}
					break;					
				}
			}
			txt += "</section>";
			txt += "<h3>" + titel + "</h3>";
			txt += "</div></a>";
			document.getElementById("film"+i).innerHTML = txt;
		}
	})
	.fail(function() {
		/* JSON fout */
		for(i = 0; i < catFilms.length; i++)
		{
			var txt = "";
			txt += "<img alt=\"error\" src=\"beeld/dave.jpg\" width=\"500\" height=\"281\">";
			txt += "<div class=\"info_film\">"
			txt += "<h3>" + connect_error + "</h3>";
			txt += "</div></a>";
			document.getElementById("film"+i).innerHTML = txt;
		}
	});
}

String.prototype.maakAfkorting = function() {
	return this.substr(0,2).maakHoofdletter() + ".";
}

function konami()
{
	$.getJSON("https://api.themoviedb.org/3/movie/74849?api_key=" + api_key, function(result)
	{
		for(i = 0; i < 9; i++)
		{
			var txt = "<a href=\"film_detail.html\" onClick=\"setCookie('film_id', '74849')\">";
				txt += "<img alt=\"" + result.title + "\" src=\"" + base_url + "w500" + result.backdrop_path + "\" width=\"500\" height=\"281\">";
				txt += "<div class=\"info_film\">"
				txt += "<section>";
				txt += "<p>20:00</p>";
				txt += "<p>Screen C</p>";	
				//console.log(alt);
				txt += "</section>";
				txt += "<h3>" + result.title + "</h3>";
				txt += "</div></a>";
				document.getElementById("film"+i).innerHTML = txt;
		}
	});
}